//
//  API.swift
//  ExtremeHealth
//
//  Created by Zhongkai Li 李忠凯 on 2018/9/17.
//  Copyright © 2018 GanDongKeJi. All rights reserved.
//

import Moya

// MARK: - TargetType Protocol Implementation
extension APIService: TargetType {
    var baseURL: URL { return URL(string: "http://api.vhealthy.igdoi.com/")! }

    var path: String {
        switch self {
        case .signUp:
            return "api/v1/signup"
        case .getCaptcha:
            return "api/v1/send_captcha"
        case .logIn:
            return "api/v1/login"
        case .logOut:
            return "api/v1/logout"
        case .wxLogin:
            return "api/v1/wxlogin"
        case .fetchProfile, .updateProfile:
            return "api/v1/user/profile"
        case .fetchMusic:
            return "api/v1/sleep/music"
        }
    }

    var method: Moya.Method {
        switch self {
        case .signUp, .getCaptcha, .logIn:
            return .post
        case .updateProfile:
            return .put
        default:
            return .get
        }
    }

    var params: [String: Any] {
        switch self {
        // MARK: account
        case let .signUp(signupType, phoneNo, captcha, userName, password):
            return formatParams(rawParams: [
                "signup_type": signupType,
                "phone_no": phoneNo,
                "captcha": captcha,
                "user_name": userName,
                "password": password
                ])
        case let .getCaptcha(token, phoneNo, scene):
            return formatParams(rawParams: [
                "token": token,
                "phone_no": phoneNo,
                "scene": scene // signup: 1, login: 2
                ])
        case let .logIn(loginType, phoneNo, captcha, userAccount, password, remenberMe):
            return formatParams(rawParams: [
                "login_type": loginType,
                "phone_no": phoneNo,
                "captcha": captcha,
                "user_account": userAccount,
                "password": password,
                "remember_me": remenberMe
                ])
        // MRAK: wx
        case let .wxLogin(code):
            return [
                "auth_code": code
                ]
        // MARK: profile
        case let .updateProfile(gender, birthday, weight, height, avatarUrl, description, targetCalory):
            return formatParams(rawParams: [
                "gender": gender,
                "birthday": birthday,
                "weight": weight,
                "height": height,
                "avatar_url": avatarUrl,
                "description": description,
                "target_calory": targetCalory
                ])
        // MARK: sleep
        case let .fetchMusic(category):
            return [
                "category": category
            ].removeNil()
        default:
            return [String: Any]()
        }
    }

    var task: Task {
        switch self {
        case .signUp, .getCaptcha, .logIn:
            return .requestParameters(parameters: params, encoding: JSONEncoding.default)
        case .wxLogin:
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        default:
            return .requestPlain
        }
    }

    // 测试数据
    var sampleData: Data {
        return Data()
    }

    var headers: [String: String]? {
        let result = ["Content-type": "application/json"]
        switch self {
        case .fetchMusic:
            return result.merging(["token": AppUser.current.accessToken],
                                  uniquingKeysWith: { (new, _) in new })
        default:
            return result
        }
    }

    var validationType: ValidationType {
        return .successCodes
    }

    private func formatParams(rawParams: JSON) -> JSON {
        return ["sbody": rawParams.removeNil()] // 使用sbody包一层
    }
}
